package a;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import ch.ethz.ssh2.Session;

public class TerminalDialog_ extends JTabbedPane
{
	private static final long serialVersionUID = 1L;

	JPanel botPanel;
	JButton logoffButton;
	JTextArea terminalArea;

	Session sess;
	InputStream in;
	OutputStream out;

	int x, y;
	class RemoteConsumer_ extends Thread
	{
		char[][] lines = new char[y][];
		int posy = 0;
		int posx = 0;

		private void addText(byte[] data, int len)
		{
			for (int i = 0; i < len; i++)
			{
				char c = (char) (data[i] & 0xff);
				if (c == 8) // Backspace, VERASE
				{
					if (posx < 0)
						continue;
					posx--;
					continue;
				}
				if (c == '\r')
				{
					posx = 0;
					continue;
				}
				if (c == '\n')
				{
					posy++;
					if (posy >= y)
					{
						for (int k = 1; k < y; k++)
							lines[k - 1] = lines[k];
						posy--;
						lines[y - 1] = new char[x];
						for (int k = 0; k < x; k++)
							lines[y - 1][k] = ' ';
					}
					continue;
				}

				if (c < 32)
				{
					continue;
				}
				if (posx >= x)
				{
					posx = 0;
					posy++;
					if (posy >= y)
					{
						posy--;
						for (int k = 1; k < y; k++)
							lines[k - 1] = lines[k];
						lines[y - 1] = new char[x];
						for (int k = 0; k < x; k++)
							lines[y - 1][k] = ' ';
					}
				}
				if (lines[posy] == null)
				{
					lines[posy] = new char[x];
					for (int k = 0; k < x; k++)
						lines[posy][k] = ' ';
				}
				lines[posy][posx] = c;
				posx++;
			}
			StringBuffer sb = new StringBuffer(x * y);
			for (int i = 0; i < lines.length; i++)
			{
				if (i != 0)
					sb.append('\n');
				if (lines[i] != null)
				{
					sb.append(lines[i]);
				}
			}
			setContent(sb.toString(),aa);
		}
		JTextArea aa;
		public RemoteConsumer_(JTextArea pp)
		{
			aa=pp;
		}
		public void run()
		{
			byte[] buff = new byte[8192];
			try
			{
				while (true)
				{
					int len = in.read(buff);
					if (len == -1)
						return;
					addText(buff, len);
				}
			}
			catch (Exception e)
			{
			}
		}
	}
	public TerminalDialog_( String title, Session sess, int x, int y) throws IOException
	{
		this.sess = sess;
		in = sess.getStdout();
		out = sess.getStdin();
		this.x = x;
		this.y = y;
		Font f = new Font("Monospaced", Font.PLAIN, 16);
		terminalArea = new JTextArea(y, x);
		terminalArea.setSize(200, 200);
		terminalArea.setFont(f);
		terminalArea.setBackground(Color.BLACK);
		terminalArea.setForeground(Color.ORANGE);
		terminalArea.setCaretColor(Color.BLACK);
		KeyAdapter kl = new KeyAdapter()
		{
			public void keyTyped(KeyEvent e)
			{
				int c = e.getKeyChar();
				try
				{
					out.write(c);
				}
				catch (IOException e1)
				{
				}
				e.consume();
			}
		};

		terminalArea.addKeyListener(kl);
		botPanel = new JPanel();
		botPanel.add(terminalArea);
		/*tabbedPane.addTab( "Page 1", botPanel );
		loginFrame.add(tabbedPane);*/
		new RemoteConsumer_(terminalArea).start();
		//new RemoteConsumer(terminalArea).start();
	}

	public void setContent(String lines,JTextArea pole)
	{
		pole.setText(lines);
	}
}
